﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace XNAVE3D
{
    class ArvoresManager : PlansManager
    {
        List<Plano> listArvores = new List<Plano>();

        int linhas = 50, colunas = 50;

        Effect effect;

        //public ArvoresManager(Game game)
        //    : base(game)
        public ArvoresManager(ContentManager Content)
            :base(Content)
        {
            //effect = Game.Content.Load<Effect>(@"Effects/Effect1");
            effect = Content.Load<Effect>(@"Effects/Effect1");

            for (int i = 0; i < linhas; i++)
            {
                for (int e = 0; e < colunas; e++)
                {
                    //Plano temp = new Plano(new Vector3(-17 + (9 * i), 2.5f, 5 + (9 * e)), Game.Content.Load<Texture2D>(@"Texturas/TexturaArvore"), Game.Content.Load<Texture2D>(@"Texturas/ArvoreSeca"), new Vector3(5, 4.5f, 0), effect, true);

                    Plano temp = new Plano(new Vector3(Terrain.verts[i * 100 + e * 100].Position.X, Terrain.verts[i * 100 + e * 100].Position.Y + 4.5f, Terrain.verts[i * 100 + e * 100].Position.Z)
                        , Content.Load<Texture2D>(@"Texturas/TexturaArvore"), Content.Load<Texture2D>(@"Texturas/TexturaArvore2"), new Vector3(5, 4.5f, 0), effect, true);

                    listArvores.Add(temp);
                }
            }
        }

        public void Update(GameTime gameTime, Matrix view)
        {
            Vector3 cameraPos = Matrix.Invert(view).Translation;
            for (int i = 0; i < listArvores.Count - 1; i++)
            {
                Vector3 arvore1Pos = listArvores[i].myWorld.Translation;
                Vector3 arvore2Pos = listArvores[i + 1].myWorld.Translation;
                if (Vector3.Distance(arvore1Pos, cameraPos) < Vector3.Distance(arvore2Pos, cameraPos))
                {
                    Plano a = listArvores[i];
                    listArvores[i] = listArvores[i + 1];
                    listArvores[i + 1] = a;
                }
            }
            foreach (Plano a in listArvores)
            {
                a.myWorld = Matrix.CreateBillboard(a.myWorld.Translation, Matrix.Invert(view).Translation, Vector3.Up, Vector3.Zero);
                a.Update(gameTime);
            }
        }

        public void Draw(Matrix projection, Matrix view, GraphicsDevice graphicsDevice)
        {
            //Game.GraphicsDevice.BlendState = BlendState.AlphaBlend;
            graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            foreach (Plano a in listArvores)
            {
                a.Draw(view, projection, graphicsDevice);
            }
        }
    }
}



